﻿using UnityEngine;
using System.Collections;

public class EnemyTrainOnRails : BaseEnemy 
{
	public GameObject offRailsCarriage;
	public int numOfCarriages = 4;

	// Use this for initialization
	protected override void Start ()
	{
		base.Start ();

		//poolManager = GameObject.FindGameObjectWithTag ("Pooler").GetComponent<Pooler>();
		//myBulletPool = poolManager.basicBulletPool;
		startPos = new Vector3 (transform.position.x, 1.0f, transform.position.z);
		moveToPos = startPos;
		for(int i =0; i< numOfCarriages; i++)
		{
			GetComponent<LinkedCarriageCration> ().CreateCarriage();
		}

		health = 10.0f;
		minAttackDistance = 0.5f;
		playAreaLimits = new Vector3 (30.0f, 30.0f);
	}
	
	protected override void Attack()
	{
	}


	public override void WasHit(float damage)
	{
		health -= damage;
		if(health < 0)
			GetOffRails();
	}

	void GetOffRails()
	{
		GameObject.FindGameObjectWithTag ("Player").GetComponent<LinkedCarriageCration> ().CreateCarriage (offRailsCarriage);//, transform);
		GetComponent<LinkedCarriageCration> ().DestroyCarriages ();
		mySpawner.EnemyDied ();
		Destroy (gameObject);

	}

	protected override void Patrol()
	{
		float dist = Vector3.Distance (transform.position, moveToPos);
		if(dist > 0.1f)
		{
			Vector3 dir = moveToPos - transform.position;
			transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(dir), rotateSpeed * Time.deltaTime);
		}
		else
		{
			moveToPos = new Vector3(Random.Range(-playAreaLimits.x, playAreaLimits.x), 1, Random.Range(-playAreaLimits.y, playAreaLimits.y));
		}
	}
}
